﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CTManagerSoln
{
    public partial class FrmChungTuCuaToi : Form
    {
        DataTable dt;
        public FrmChungTuCuaToi()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.TopMost = true;
        }

        private void FrmChungTuCuaToi_Load(object sender, EventArgs e)
        {
            string dateFrom = fromdate.Value.Date.AddDays(-1).ToLocalTime().ToShortDateString();
            string dateTo = todate.Value.Date.ToLocalTime().ToShortDateString();
            string tenChungTu = txttenchungtu.Text;
            LoadComboLoaiChungTu();
            string maloaiChungTu = comboLoaichungtu.SelectedValue.ToString();
            string status = "";
            LoadChungTu(dateFrom, dateTo, Commons.UsernameLogin, tenChungTu, maloaiChungTu, status);
            chcknhap.Checked = true;
        }

        private void LoadChungTu(string datefrom,string dateto,string username,string tenChungTu,string maLoaiChungTu,string status)
        {
            dt = clsChungtu.GetChungTu(datefrom, dateto, username, tenChungTu, maLoaiChungTu, status);
           gvChungtuNhap.DataSource = dt;
        }

        private void LoadComboLoaiChungTu()
        {
            comboLoaichungtu.DataSource = clsLoaichungtu.getLoaiChungTu();
            comboLoaichungtu.DisplayMember = "TenLoaiChungTu";
            comboLoaichungtu.ValueMember = "MaLoaiChungTu";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string dateFrom = fromdate.Value.Date.ToLocalTime().ToShortDateString();
            string dateTo = todate.Value.Date.ToLocalTime().ToShortDateString();
            string tenChungTu = txttenchungtu.Text;
            string maloaiChungTu = comboLoaichungtu.SelectedValue.ToString();
            if (chcknhap.Checked)//chung tu cua toi
            {
                LoadChungTu(dateFrom, dateTo, Commons.UsernameLogin, tenChungTu, maloaiChungTu,"");
            }
            else//chung tu da duyet--cho duyet
            {

            }
        }

        private void chcknhap_CheckedChanged(object sender, EventArgs e)
        {
            chckchoduyet.Checked = false;
            chckduyet.Checked = false;
        }

        private void chckduyet_CheckedChanged(object sender, EventArgs e)
        {
            chcknhap.Checked = false;
            chckchoduyet.Checked = false;
        }

        private void chckchoduyet_CheckedChanged(object sender, EventArgs e)
        {
            chcknhap.Checked = false;
            chckduyet.Checked = false;
           
        }

        void gvChungtuNhap_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.ColumnIndex == 4 && e.RowIndex != -1)
            {
                //Create the ContextStripMenu for Creating the PO Sub Form
                ContextMenuStrip Menu = getContextmenu();
                ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
                MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_MouseDown);
                Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });

                //Assign created context menu strip to the DataGridView
                gvChungtuNhap.ContextMenuStrip = Menu;
                CellValue = gvChungtuNhap.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }

            else
                gvChungtuNhap.ContextMenuStrip = null;
        }

        private ContextMenuStrip getContextmenu()
        {
            ContextMenuStrip mnu = new ContextMenuStrip();
            ToolStripMenuItem mnuxemchungtu = new ToolStripMenuItem("Xem chứng từ");
            ToolStripMenuItem mnuchuyenChungTu = new ToolStripMenuItem("chuyển chứng từ");
            ToolStripMenuItem mnuduyetchungtu = new ToolStripMenuItem("Duyệt chứng từ");
            //Assign event handlers            
            mnuchuyenChungTu.Click += new EventHandler(mnuchuyenChungTu_Click);
            mnuxemchungtu.Click += new EventHandler(mnuxemchungtu_Click);
            mnuduyetchungtu.Click += new EventHandler(mnuduyetchungtu_Click);
            //Add to main context menu
            if (!Commons.isKiemDuyet)//khong co quyen kiem duyet
            {
                mnu.Items.AddRange(new ToolStripItem[] { mnuxemchungtu, mnuchuyenChungTu});
            }
            else
            {
                mnu.Items.AddRange(new ToolStripItem[] { mnuxemchungtu, mnuchuyenChungTu, mnuduyetchungtu });
            }
            
            return mnu;
        }

        void mnuduyetchungtu_Click(object sender, EventArgs e)
        {
            
        }

        void mnuxemchungtu_Click(object sender, EventArgs e)
        {
            FrmNhapChungtu frm = new FrmNhapChungtu(true,CellValue);
            frm.Show();
        }

        void mnuchuyenChungTu_Click(object sender, EventArgs e)
        {
            //get data nguoi co quyen kiem duyet cua user dang login
            DataTable dt=ClsUserphongban.getUserKiemDuyet(Commons.UsernameLogin);

            DialogResult abc = MessageBox.Show("chuyển chứng từ này đến " + dt.Rows[0]["UserName"], "xác nhận chuyển chứng từ", MessageBoxButtons.YesNoCancel);

            string sohieuChungTu = CellValue;
            string nguoichuyen = Commons.UsernameLogin;
            string tuphongban = dt.Rows[0]["MaPhongBan"].ToString();
            string nguoinhan = dt.Rows[0]["UserName"].ToString();
            DateTime ngaychuyen = DateTime.UtcNow;
            DateTime ngaykiemduyet = DateTime.UtcNow;
            string ghichu = "";
            bool isApprove = false;


            if (abc==DialogResult.Yes)
            {
                //goi ham chuyen
                clsKiemDuyet.inSert(sohieuChungTu, nguoichuyen, tuphongban, nguoinhan, ngaychuyen, ngaykiemduyet, ghichu, isApprove);
                clsChungtu.Update(sohieuChungTu,true);
            }
        }
        private void gvChungtuNhap_MouseDown(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Right)
            //{
            //    CellValue = gvChungtuNhap.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            //    var ht = gvChungtuNhap.HitTest(e.X, e.Y);
            //    if (ht.ColumnIndex == 4 && ht.RowIndex != -1)
            //    {
            //        //Create the ContextStripMenu for Creating the PO Sub Form
            //        ContextMenuStrip Menu = new ContextMenuStrip();
            //        ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
            //        MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_MouseDown);
            //        Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });

            //        //Assign created context menu strip to the DataGridView
            //        gvChungtuNhap.ContextMenuStrip = Menu;
            //    }

            //    else
            //        gvChungtuNhap.ContextMenuStrip = null;
            //}
        }

        void MenuOpenPO_MouseDown(object sender, MouseEventArgs e)
        {
            MessageBox.Show("teest","thong bao");
        }

        public string CellValue { get; set; }


        private void gvChungtuNhap_CellMouseDown_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                //Create the ContextStripMenu for Creating the PO Sub Form
                ContextMenuStrip Menu = getContextmenu();
                //ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
                //MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_MouseDown);
                //Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });

                //Assign created context menu strip to the DataGridView
                gvChungtuNhap.ContextMenuStrip = Menu;
                if (gvChungtuNhap.Rows[e.RowIndex].Cells[e.ColumnIndex].Value!=null)
                {
                   CellValue = gvChungtuNhap.Rows[e.RowIndex].Cells["SoHieuChungTu"].Value.ToString();
                }                
            }

            else
                gvChungtuNhap.ContextMenuStrip = null;
        }
    }
}
